﻿using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace System
{
    public static class Objectifyer
    {
        
        public static object Clone(this object o)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, o);
            ms.Position = 0;
            object obj = bf.Deserialize(ms);
            ms.Close();
            return obj;
        }

        public static string Base64Encode(string data)
        {

            byte[] encData_byte = new byte[data.Length];
            encData_byte = Encoding.UTF8.GetBytes(data);
            string encodedData = Convert.ToBase64String(encData_byte);
            return encodedData;
        }

        public static string Base64Decode(string data)
        {

            UTF8Encoding encoder = new UTF8Encoding();
            Decoder utf8Decode = encoder.GetDecoder();

            byte[] todecode_byte = Convert.FromBase64String(data);
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string result = new String(decoded_char);
            return result;

        }
        [DebuggerStepThrough]
        public static object FromXml(object container, string xml)
        {
            return FromXml(xml, container.GetType(), null);
        }

        [DebuggerStepThrough]
        public static object FromXml(string Xml, Type ObjType, Type[] extraTypes)
        {
            XmlSerializer ser;

            if (extraTypes == null)
                ser = new XmlSerializer(ObjType);
            else
                ser = new XmlSerializer(ObjType, extraTypes);
            StringReader stringReader;
            stringReader = new StringReader(Xml);
            XmlTextReader xmlReader;
            xmlReader = new XmlTextReader(stringReader);
            object obj;
            obj = ser.Deserialize(xmlReader);
            xmlReader.Close();
            stringReader.Close();
            return obj;

        }


        [DebuggerStepThrough]
        public static string ToDataContractXml(Object Obj)
        { return ToDataContractXml(Obj, Obj.GetType(), null); }

        [DebuggerStepThrough]
        public static string ToDataContractXml(object Obj, Type ObjType, Type[] extraTypes)
        {

            DataContractSerializer ser;
            if (extraTypes == null)
                ser = new DataContractSerializer(ObjType);
            else
                ser = new DataContractSerializer(ObjType, extraTypes);
            MemoryStream memStream;
            memStream = new MemoryStream();
            XmlTextWriter xmlWriter;
            xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            xmlWriter.Namespaces = true;
            ser.WriteObject(xmlWriter, Obj);

            xmlWriter.Close();
            memStream.Close();
            string xml;
            xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            //xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            //xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            return xml;



        }
        public static string FromFile(string fileName)
        {
            string buffer = string.Empty;
            using (StreamReader streamReader = File.OpenText(fileName))
            {
                buffer = streamReader.ReadToEnd();
            }

            return buffer;
        }

        public static void ToFile(string fileContents, string fileName)
        {
            using (StreamWriter sw = File.CreateText(fileName))
            {
                sw.Write(fileContents);
                sw.Flush();
                sw.Close();

            }

        }

        [DebuggerStepThrough]
        public static string ToXml(Object Obj)
        {
            return ToXml(Obj, Obj.GetType(), null);
        }

        [DebuggerStepThrough]
        public static string ToXml(object Obj, Type ObjType, Type[] extraTypes)
        {

            XmlSerializer ser;
            if (extraTypes == null)
                ser = new XmlSerializer(ObjType);
            else
                ser = new XmlSerializer(ObjType, extraTypes);
            MemoryStream memStream;
            memStream = new MemoryStream();
            XmlTextWriter xmlWriter;
            xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            xmlWriter.Namespaces = true;
            ser.Serialize(xmlWriter, Obj);
            xmlWriter.Close();
            memStream.Close();
            string xml;
            xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            //xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            //xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            return xml;

        }


    }
}
